package cn.itcast.controller.stat;

import cn.itcast.controller.BaseController;
import cn.itcast.service.stat.StatService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stat")
public class StatController extends BaseController {
    @Reference
    private StatService statService;

    @RequestMapping("/toCharts")
    public Object toCharts(String chartsType) {
        return "stat/stat-" + chartsType;
    }

    @RequestMapping("/factoryCharts")
    @ResponseBody
    public Map factory(){
        List<Map> list = statService.findFactoryAmount(getCompanyId());
        List title = new ArrayList();
        Map data = new HashMap<>();
        for (Map map : list) {
            title.add(map.get("name"));
        }
        data.put("title",title);
        data.put("data",list);
        return data;
    }

    @RequestMapping("/onlineCharts")
    @ResponseBody
    public Map online(){
        List<Map> list = statService.findOnlineTime(getCompanyId());
        List title = new ArrayList();
        List value = new ArrayList();
        Map data = new HashMap<>();
        for (Map map : list) {
            title.add(map.get("name"));
            value.add(map.get("value"));
        }
        data.put("title",title);
        data.put("value",value);
        return data;
    }

    @RequestMapping("/sellCharts")
    @ResponseBody
    public Map sell(){
        List<Map> list = statService.findSellProducts(getCompanyId());
        List title = new ArrayList();
        List value = new ArrayList();
        Map data = new HashMap<>();
        for (Map map : list) {
            title.add(map.get("name"));
            value.add(map.get("value"));
        }
        data.put("title",title);
        data.put("value",value);
        return data;
    }
}
